import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

import java.util.Random;

import org.junit.Test;

import editortrees.EditTree;


/**
 * TODO Put here a description of what this class does.
 *
 * @author timaeudg.
 *         Created Oct 23, 2012.
 */
public class LargeTests {
	
	@Test
	public void testAddMany() {
		EditTree t = new EditTree();
		for (int k = 0; k < 100000; k++) {
			for (int j = 0; j < 10; j++) {
				char toAdd = (char) ('0' + j);
				t.add(toAdd);
			}
		}
		assertEquals(19, t.height());
		assertEquals(1000000, t.size());
		System.out.println(t.totalRotationCount());
	}

	@Test
	public void testAddManyRandom() {
		EditTree t = new EditTree();
		Random random = new Random();
		for (int k = 0; k < 100000; k++) {
			for (int j = 0; j < 10; j++) {
				char toAdd = (char) ('0' + j);
				t.add(toAdd, random.nextInt(10*k+j + 1));
			}
		}
		assertEquals(1000000, t.size());
		int height = t.height();
		int max = (int) Math.ceil(1.44 * (Math.log(1000000) / Math.log(2)));
		int min = (int) Math.floor(Math.log(1000000) / Math.log(2));
		boolean heightCheck = false;
		if (height >= min && height <= max) {
			heightCheck = true;
		}
		assertTrue(heightCheck);
	}


	@Test
	public void testDeleteManyRandom() {
		EditTree t = new EditTree();
		Random random = new Random();
		for (int k = 0; k < 100000; k++) {
			for (int j = 0; j < 10; j++) {
				char toAdd = (char) ('0' + j);
				t.add(toAdd, random.nextInt(10*k+j + 1));
			}
		}
		for(int k =0; k<100000;k++){
			for(int j =0; j < 9; j++){
				t.delete(random.nextInt(1000000-(9*k+j)-1));
			}
		}
		assertEquals(100000, t.size());
		int height = t.height();
		int max = (int) Math.ceil(1.44 * (Math.log(1000000) / Math.log(2)));
		int min = (int) Math.floor(Math.log(1000000) / Math.log(2));
		boolean heightCheck = false;
		if (height >= min && height <= max) {
			heightCheck = true;
		}
		assertTrue(heightCheck);
	}
	
	@Test
	public void testDeleteMany() {
		EditTree t = new EditTree();
		for (int k = 0; k < 100000; k++) {
			for (int j = 0; j < 10; j++) {
				char toAdd = (char) ('0' + j);
				t.add(toAdd);
			}
		}
		for(int k =0; k<100000;k++){
			for(int j =0; j < 9; j++){
				t.delete(1000000-(9*k+j)-1);
			}
		}
		assertEquals(100000, t.size());
		assertEquals(16, t.height());
		System.out.println(t.totalRotationCount());
	}
	
}
